<template>
    <div class="role-list">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form ref="form" :model="form">
            <el-form-item label="角色名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" v-loading="isLoading">查询</el-button>
              <el-button @click="onReset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-button @click="handleAdd">添加角色</el-button>
        <el-table
          :data="roles"
          style="width: 100%"
          v-loading="isLoading">
          <el-table-column
            prop="id"
            label="编号">
          </el-table-column>
          <el-table-column
            prop="name"
            label="角色名称">
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述">
          </el-table-column>
          <el-table-column
            label="添加时间">
            <template slot-scope="scope">
                <span>{{ scope.row.createdTime | dateFormate }}</span>
              </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="150px">
            <template slot-scope="scope">
              <div>
                <el-button type="text" @click="$router.push({name: 'alloc-menu', params: {roleId: scope.row.id}})">分配菜单</el-button>
                <el-button type="text">分配资源</el-button>
              </div>
              <div>
                <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!-- 添加角色的对话框结构 -->
        <el-dialog :title="isEdit ? '编辑角色' : '添加角色'" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
          <create-or-edit v-if="dialogVisible" :role-id="roleId" :is-edit="isEdit" @success="handleSuccess" @cancel="handleCancel"></create-or-edit>
        </el-dialog>
      </el-card>
    </div>
</template>

<script>
import { getRoles, deleteRole } from '@/services/role'
import CreateOrEdit from './CreateOrEdit.vue'
export default {
  components: { CreateOrEdit },
  name: 'RoleLIst',
  data () {
    return {
      form: {
        name: ''
      },
      roles: [],
      isLoading: false,
      dialogVisible: false,
      isEdit: false,
      // 正在编辑的id
      roleId: ''
    }
  },
  created () {
    this.loadRoles()
  },
  methods: {
    handleEdit (role) {
      this.dialogVisible = true
      this.isEdit = true
      this.roleId = role.id
    },
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    },
    // 监听子组件取消
    handleCancel () {
      // 关闭dialog
      this.dialogVisible = false
    },
    // 监听子组件成功
    handleSuccess () {
      // 关闭dialog
      this.dialogVisible = false
      // 刷新列表
      this.loadRoles()
    },
    async loadRoles () {
      this.isLoading = true
      const { data } = await getRoles(this.form)
      if (data.code === '000000') {
        this.roles = data.data.records
        this.isLoading = false
      }
    },
    handleDelete (role) {
      this.$confirm(`确认删除角色: ${role.name}?`, '删除提示')
        .then(async () => {
          await deleteRole(role.id)
          this.$message.success('删除成功')
          this.loadRoles()
        })
        .catch(err => {
          if (err && err.response) {
            this.$message.error('删除失败，请重试')
          } else {
            this.$message.info('取消删除')
          }
        })
    },
    onSubmit () {

    },
    onReset () {
    }
  },
  filters: {
    dateFormate (date) {
      console.log(date)
      date = new Date(date)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
              ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
  }
}
</script>

<style lang="scss" scoped ></style>
